if !exists(param.A)
  M291 S1 T0 R"Use Auto Calibration macro" P"Using separate calibration macros may cause issues.<br>Please use the Auto Calibration macro"
  abort "Error: Auto Calibration macro required."
















M98 P"0:/sys/detachedcheck.g" ; Check if the probe is detached

var Probe_Point_Y = 98
var max_Travel = 15
var offset = 3
var Z_Dive = 0.5

;==== T0 Z refference point ====
G90
G1 X-20 U999 Y80 Z2 F12000
M98 P"0:/sys/nozzleprobe.g" Z1
var T0_Ref_Z = move.axes[2].userPosition

G90                                                    ; Relative
G1 Z{var.T0_Ref_Z + 1}
G1 Y{var.Probe_Point_Y} X0 F12000                                       ;Move to the Centre
G1 Z{var.T0_Ref_Z - var.Z_Dive}                        ; Move down the Bed Hole


; ===== Single Probe with T0 =====
M558 K0 P8 C"1.io4.in" H5 F60 T18000              ; define Z probe parameters
M98 P"0:/user/probeoffset.g"                       ; load global variables

; 1st Probe
G38.4 K0 Y{move.axes[1].userPosition - var.max_Travel} ; Probe LY-
M400
G4 P260
var T0_Front = move.axes[1].userPosition

G91
G1 Y{var.offset} F12000
G90

; 2nd Probe
G38.4 K0 Y{move.axes[1].userPosition + var.max_Travel} ; Probe LY+
M400
G4 P260
var T0_Rear = move.axes[1].userPosition

var T0_center_Y = {var.T0_Front + var.T0_Rear} / 2

G90
G1 Y{var.T0_center_Y} X0


; 3rd Probe
G38.4 K0 X{move.axes[0].userPosition - var.max_Travel} ; Probe LX-
M400
G4 P260
var T0_Left = move.axes[0].userPosition

G91
G1 X{var.offset} F12000
G90

; 4th Probe
G38.4 K0 X{move.axes[0].userPosition + var.max_Travel} ; Probe LX-
M400
G4 P260
var T0_Right = move.axes[0].userPosition


var T0_center_X = {var.T0_Left + var.T0_Right} / 2
G90
G1 X{var.T0_center_X} Y{var.T0_center_Y}


; ===== Final Probe with T0 =====

; 1st Probe
G90
G1 X{var.T0_center_X} Y{var.T0_Front + var.offset}
G38.4 K0 Y{move.axes[1].userPosition - var.max_Travel}
set var.T0_Front = move.axes[1].userPosition
;echo >>"0:/macros/report.txt" "T0 Y- X"^{move.axes[0].machinePosition}^" Y"^{move.axes[1].machinePosition}^" Z"^{move.axes[2].machinePosition}^" U"^{move.axes[3].machinePosition}

; 2nd Probe
G1 X{var.T0_center_X} Y{var.T0_Rear - var.offset}
G38.4 K0 Y{move.axes[1].userPosition + var.max_Travel}
set var.T0_Rear = move.axes[1].userPosition
;echo >>"0:/macros/report.txt" "T0 Y+ X"^{move.axes[0].machinePosition}^" Y"^{move.axes[1].machinePosition}^" Z"^{move.axes[2].machinePosition}^" U"^{move.axes[3].machinePosition}

set var.T0_center_Y = {var.T0_Front + var.T0_Rear} / 2

; 3rd Probe
G1 X{var.T0_Left + var.offset} Y{var.T0_center_Y}
G38.4 K0 X{move.axes[0].userPosition - var.max_Travel}
set var.T0_Left = move.axes[0].userPosition
;echo >>"0:/macros/report.txt" "T0 X- X"^{move.axes[0].machinePosition}^" Y"^{move.axes[1].machinePosition}^" Z"^{move.axes[2].machinePosition}^" U"^{move.axes[3].machinePosition}

; 4th Probe
G1 X{var.T0_Right - var.offset} Y{var.T0_center_Y}
G38.4 K0 X{move.axes[0].userPosition + var.max_Travel}
set var.T0_Right = move.axes[0].userPosition
;echo >>"0:/macros/report.txt" "T0 X+ X"^{move.axes[0].machinePosition}^" Y"^{move.axes[1].machinePosition}^" Z"^{move.axes[2].machinePosition}^" U"^{move.axes[3].machinePosition}

set var.T0_center_X = {var.T0_Left + var.T0_Right} / 2

G1 X{var.T0_center_X} Y{var.T0_center_Y} Z{var.T0_Ref_Z}
G4 S1
G1 Z{var.T0_Ref_Z + 5}


















T1 ; Select Tool 1 before wiping
M98 P"0:/sys/nozzlewipe.g" T1
T0 ; Return to Tool 0

;==== T1 Z refference point ====
G90
G1 X-999 U20 Y80 Z{var.T0_Ref_Z + 3} F12000
M98 P"0:/sys/nozzleprobe.g" Z10
var T1_Ref_Z = move.axes[2].userPosition

G90                                                    ; Relative
G1 Z{var.T1_Ref_Z + 1}
G1 Y100 U0 F12000                                      ;Move to the Centre
G1 Z{var.T1_Ref_Z - var.Z_Dive}                        ; Move down the Bed Hole


; ===== Single Probe with T1 =====
M558 K0 P8 C"1.io4.in" H5 F60 T18000              ; define Z probe parameters
M98 P"0:/user/probeoffset.g"                       ; load global variables

; 1st Probe
G38.4 K0 Y{move.axes[1].userPosition - var.max_Travel} ; Probe LY-
M400
G4 P260
var T1_Front = move.axes[1].userPosition

G91
G1 Y{var.offset} F12000
G90

; 2nd Probe
G38.4 K0 Y{move.axes[1].userPosition + var.max_Travel} ; Probe LY+
M400
G4 P260
var T1_Rear = move.axes[1].userPosition

var T1_center_Y = {var.T1_Front + var.T1_Rear} / 2

G90
G1 Y{var.T1_center_Y} U0


; 3rd Probe
G38.4 K0 U{move.axes[3].userPosition - var.max_Travel} ; Probe LX-
M400
G4 P260
var T1_Left = move.axes[3].userPosition

G91
G1 U{var.offset} F12000
G90

; 4th Probe
G38.4 K0 U{move.axes[3].userPosition + var.max_Travel} ; Probe LX-
M400
G4 P260
var T1_Right = move.axes[3].userPosition


var T1_center_X = {var.T1_Left + var.T1_Right} / 2
G90
G1 U{var.T1_center_X} Y{var.T1_center_Y}


; ===== Final Probe with T1 =====

; 1st Probe
G90
G1 U{var.T1_center_X} Y{var.T1_Front + var.offset}
G38.4 K0 Y{move.axes[1].userPosition - var.max_Travel}
set var.T1_Front = move.axes[1].userPosition

; 2nd Probe
G1 U{var.T1_center_X} Y{var.T1_Rear - var.offset}
G38.4 K0 Y{move.axes[1].userPosition + var.max_Travel}
set var.T1_Rear = move.axes[1].userPosition


set var.T1_center_Y = {var.T1_Front + var.T1_Rear} / 2


; 3rd Probe
G1 U{var.T1_Left + var.offset} Y{var.T1_center_Y}
G38.4 K0 U{move.axes[3].userPosition - var.max_Travel}
set var.T1_Left = move.axes[3].userPosition

; 4th Probe
G1 U{var.T1_Right - var.offset} Y{var.T1_center_Y}
G38.4 K0 U{move.axes[3].userPosition + var.max_Travel}
set var.T1_Right = move.axes[3].userPosition

set var.T1_center_X = {var.T1_Left + var.T1_Right} / 2
G1 U{var.T1_center_X} Y{var.T1_center_Y} Z{var.T1_Ref_Z}
G4 S1
G1 Z{var.T1_Ref_Z + 5}





; Calculate offsets
set global.uoffset = var.T0_center_X - var.T1_center_X
set global.yoffset = var.T0_center_Y - var.T1_center_Y


G91
G1 Z5 F12000
G90
M400

echo "Current offsets: U"^{global.uoffset}^" Y"^{global.yoffset}^" Z"^{global.rtzoffset}



; Generate uoffset.g
echo >"0:/user/uoffset.g" "if exists(global.uoffset)"
echo >>"0:/user/uoffset.g" "  set global.uoffset = "^{global.uoffset}
echo >>"0:/user/uoffset.g" "else"
echo >>"0:/user/uoffset.g" "  global uoffset = "^{global.uoffset}

; Generate yoffset.g
echo >"0:/user/yoffset.g" "if exists(global.yoffset)"
echo >>"0:/user/yoffset.g" "  set global.yoffset = "^{global.yoffset}
echo >>"0:/user/yoffset.g" "else"
echo >>"0:/user/yoffset.g" "  global yoffset = "^{global.yoffset}

; Generate rtzoffset.g
echo >"0:/user/rtzoffset.g" "if exists(global.rtzoffset)"
echo >>"0:/user/rtzoffset.g" "  set global.rtzoffset = "^{global.rtzoffset}
echo >>"0:/user/rtzoffset.g" "else"
echo >>"0:/user/rtzoffset.g" "  global rtzoffset = "^{global.rtzoffset}


; Generate tooloffset.g
echo >"0:/user/tooloffset.g" "                         ; Set tool offsets"
echo >>"0:/user/tooloffset.g" "G10 P1 U"^{global.uoffset}^" Y"^{global.yoffset}^" Z"^{global.rtzoffset}
;echo >>"0:/macros/report.txt" "G10 P1 U"^{global.uoffset}^" Y"^{global.yoffset}^" Z"^{global.rtzoffset}

; Apply changes
G10 P1 U{global.uoffset} Y{global.yoffset} Z{global.rtzoffset}

M204 T5000